<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="商户号" prop="merId">
        <el-input
          v-model="queryParams.merId"
          placeholder="请输入商户号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="银行账号" prop="bankAccount">
        <el-input
          v-model="queryParams.bankAccount"
          placeholder="请输入银行账号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开户行" prop="bankName">
        <el-input
          v-model="queryParams.bankName"
          placeholder="请输入开户行"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['merchant:bankMerchant:add']"
        >新增
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['merchant:bankMerchant:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['merchant:bankMerchant:export']"
        >导出
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="bankMerchantList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="商户号" align="center" prop="merId"/>
      <el-table-column label="银行类型" align="center" prop="bankType"
                       :formatter="bankTypeFormat"/>
      <el-table-column label="银行账号" align="center" prop="bankAccount"/>
      <el-table-column label="银行账号名称" align="center" prop="bankAccountName"/>
      <el-table-column label="银行子账号" align="center" prop="subBankAccount"/>
      <el-table-column label="开户行" align="center" prop="bankName"/>
      <el-table-column label="省市代码" align="center" prop="dbProv"/>
      <el-table-column label="币种" align="center" prop="dbCur"/>
      <el-table-column label="账户余额" align="center" prop="bankBalance"/>
      <el-table-column label="操作" align="center" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleViewUpdate(scope.row,'view')"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleViewUpdate(scope.row,'edit')"
            v-hasPermi="['merchant:bankMerchant:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['merchant:bankMerchant:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改银行账户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="商户号" prop="merId">
          <el-input v-model="form.merId" placeholder="请输入商户号" :disabled="isEdit"/>
        </el-form-item>
        <el-form-item label="银行类型" prop="bankType">
          <el-select filterable v-model="form.bankType" placeholder="请选择银行类型" :disabled="isEdit">
            <el-option
              v-for="dict in bankTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="银行账号" prop="bankAccount">
          <el-input v-model="form.bankAccount" placeholder="请输入银行账号"/>
        </el-form-item>
        <el-form-item label="银行账号名称" prop="bankAccountName">
          <el-input v-model="form.bankAccountName" placeholder="请输入银行账号名称"/>
        </el-form-item>
        <el-form-item label="银行子账号" prop="subBankAccount">
          <el-input v-model="form.subBankAccount" placeholder="请输入银行账号"/>
        </el-form-item>
        <el-form-item label="开户行" prop="bankName">
          <el-input v-model="form.bankName" placeholder="请输入开户行"/>
        </el-form-item>
        <el-form-item label="省市代码" prop="dbProv">
          <el-input v-model="form.dbProv" placeholder="请输入省市代码"/>
        </el-form-item>
        <el-form-item label="币种" prop="dbCur">
          <el-input v-model="form.dbCur" placeholder="请输入币种"/>
        </el-form-item>
        <el-form-item label="账户余额" prop="bankBalance">
          <el-input v-model="form.bankBalance" placeholder="请输入账户余额"/>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="title !== '查看银行账户'">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listBankMerchant,
    getBankMerchant,
    delBankMerchant,
    addBankMerchant,
    updateBankMerchant,
    exportBankMerchant
  } from "@/api/merchant/bankMerchant";

  export default {
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        merIds: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 银行账户表格数据
        bankMerchantList: [],
        // 弹出层标题
        title: "",
        //是否是编辑状态
        isEdit: false,
        // 是否显示弹出层
        open: false,
        // 银行类型字典
        bankTypeOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          merId: undefined,
          bankAccount: undefined,
          bankName: undefined,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          merId: [
            {required: true, message: "商户号不能为空", trigger: "blur"}
          ], bankType: [
            {required: true, message: "银行类型不能为空", trigger: "change"}
          ], bankAccount: [
            {required: true, message: "银行账号不能为空", trigger: "blur"},
            {pattern: /^([1-9]{1})(\d{11}|\d{14}|\d{15}|\d{16}|\d{17}|\d{18}|\d{19})$/, message: '银行账号格式不正确'}
          ], bankAccountName: [
            {required: true, message: "银行账号名称不能为空", trigger: "blur"}
          ], subBankAccount: [
            {required: true, message: "银行子账号不能为空", trigger: "blur"}
          ]
          , bankName: [
            {required: true, message: "开户行不能为空", trigger: "blur"}
          ], dbProv: [
            {required: true, message: "省市代码不能为空", trigger: "blur"}
          ], dbCur: [
            {required: true, message: "币种不能为空", trigger: "blur"}
          ],bankBalance:[
            {pattern: /(^[1-9]{1}[0-9]*$)|(^[0-9]*\.[0-9]{2}$)/, message: '账户余额格式不正确'}
          ]
        }
      };
    },
    created() {
      this.getList();
      this.getDicts("bank_type").then(response => {
        this.bankTypeOptions = response.data;
      });
    },
    methods: {
      /** 查询银行账户列表 */
      getList() {
        this.loading = true;
        listBankMerchant(this.queryParams).then(response => {
          this.bankMerchantList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 银行类型字典翻译
      bankTypeFormat(row, column) {
        return this.selectDictLabel(this.bankTypeOptions, row.bankType);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          merId: undefined,
          bankType: undefined,
          bankAccount: undefined,
          subBankAccount: undefined,
          bankAccountName: undefined,
          bankName: undefined,
          dbProv: undefined,
          dbCur: undefined,
          bankBalance: undefined,
          corpCode: undefined,
          corpName: undefined,
          createBy: undefined,
          createDate: undefined,
          reqIp: undefined,
          status: "0",
          updateBy: undefined,
          updateDate: undefined,
          remarks: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.merIds = selection.map(item => item.merId)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.isEdit = false;
        this.reset();
        this.open = true;
        this.title = "添加银行账户";
      },
      /** 修改按钮操作 */
      handleViewUpdate(row, type) {
        this.reset();
        const id = row.id || this.ids
        getBankMerchant(id).then(response => {
          this.form = response.data;
          this.open = true;
          if ('edit' === type) {
            this.title = "修改银行账户";
            this.isEdit = true;
          }
          if ('view' === type) {
            this.title = "查看银行账户";
            this.isEdit = false;
          }
        });
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateBankMerchant(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              addBankMerchant(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        const merIds = row.merId || this.merIds;
        this.$confirm('是否确认数据?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delBankMerchant(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出银行账户数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportBankMerchant(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function () {
        });
      }
    }
  };
</script>
